/*
 * @author : TF-BJ-C064
 * @creation : 2014-8-7 下午7:54:55
 * @description : 
 *
 */

package com.bean.billtool;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 停车场白天(晚上)上班(下班)时间校验
 * <p>Title: ParkTimeValidate</p>
 * <p>Description:
 * 		接口  com.teleframe.bill.validate()
 *  </p>
 * <p>Company: www.teleframe.cn</p> 
 * @author	lijian
 * @date	2014-8-7
 */
public class ParkTimeValidate {

	public static boolean DEBUG = false;

	private String dayStartTime;
	private String dayEndTime;
	private String nightStartTime;
	private String nightEndTime;

	Date dDayStartTime;
	Date dDayEndTime;
	Date dNightStartTime;
	Date dNightEndTime;

	SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

	public String toString(){
		return "{"+dayStartTime+","+dayEndTime+"} - {"+nightStartTime+","+nightEndTime+"}";
	}

	public static void main(String argv[]){
		ParkTimeValidate ptv = new ParkTimeValidate();

		System.out.println("================================  白天上班时间检查 =================================");
		System.out.println(ptv.toString());
		System.out.println( ptv.validate("23230", ParkTimeType.DAY_START_TIME) );
		System.out.println( ptv.validate("4:00", ParkTimeType.DAY_START_TIME) );
		System.out.println( ptv.validate("5:00", ParkTimeType.DAY_START_TIME) );
		System.out.println( ptv.validate("6:00", ParkTimeType.DAY_START_TIME) );// this is dDayStartTime

		System.out.println("================================  白天下班时间检查 =================================");
		System.out.println(ptv.toString());
		System.out.println( ptv.validate("4:fsdf", ParkTimeType.DAY_END_TIME) );
		System.out.println( ptv.validate("4:00", ParkTimeType.DAY_END_TIME) );
		System.out.println( ptv.validate("6:00", ParkTimeType.DAY_END_TIME) );
		System.out.println( ptv.validate("18:00", ParkTimeType.DAY_END_TIME) );// this is dDayEndTime

		System.out.println("================================  晚上上班时间检查 =========================================");
		System.out.println(ptv.toString());
		System.out.println( ptv.validate("1232", ParkTimeType.NIGHT_START_TIME) );
		System.out.println( ptv.validate("4:00", ParkTimeType.NIGHT_START_TIME) );
		System.out.println( ptv.validate("6:00", ParkTimeType.NIGHT_START_TIME) );
		System.out.println( ptv.validate("8:00", ParkTimeType.NIGHT_START_TIME) );// this is dNightStartTime
		System.out.println( ptv.validate("20:00", ParkTimeType.NIGHT_START_TIME) );// this is dNightStartTime
		System.out.println( ptv.validate("1:00", ParkTimeType.NIGHT_START_TIME) );// this is dNightStartTime

		System.out.println("================================  晚上下班时间检查 =================================");
		System.out.println(ptv.toString());
		System.out.println( ptv.validate("3424", ParkTimeType.NIGHT_END_TIME) );
		System.out.println( ptv.validate("4:00", ParkTimeType.NIGHT_END_TIME) );
		System.out.println( ptv.validate("6:00", ParkTimeType.NIGHT_END_TIME) );
		System.out.println( ptv.validate("8:00", ParkTimeType.NIGHT_END_TIME) );// this is dNightEndTime
		System.out.println( ptv.validate("18:00", ParkTimeType.NIGHT_END_TIME) );// this is dNightEndTime
		System.out.println( ptv.validate("23:00", ParkTimeType.NIGHT_END_TIME) );// this is dNightEndTime

		ptv.setNightStartTime("20:00");
		ptv.setNightEndTime(null);
		System.out.println("\n"+ ptv.toString());
		System.out.println( ptv.validate("4:00", ParkTimeType.NIGHT_END_TIME) );
		System.out.println( ptv.validate("6:00", ParkTimeType.NIGHT_END_TIME) );
		System.out.println( ptv.validate("8:00", ParkTimeType.NIGHT_END_TIME) );// this is dNightEndTime
		System.out.println( ptv.validate("18:00", ParkTimeType.NIGHT_END_TIME) );// this is dNightEndTime
		System.out.println( ptv.validate("23:00", ParkTimeType.NIGHT_END_TIME) );// this is dNightEndTime

	}

	/**
	 * 停车场白天/晚上上下班时间校验
	 * <p>Title: validate</p>
	 * <p>Description: </p>
	 * @param checkTimeIn  待校验的时间
	 * @param timeType 时间类型  see com.teleframe.bill.ParkTimeType
	 * @return
	 * 		成功 ： null  
	 * 		失败 : 返回错误字符串
	 */
	public String validate(String checkTimeIn, ParkTimeType timeType){
		System.out.print(checkTimeIn + "  ");
		switch(timeType){
		case DAY_START_TIME:
			return this.validateDayStartTime(checkTimeIn);
		case DAY_END_TIME:
			return this.validateDayEndTime(checkTimeIn);
		case NIGHT_START_TIME:
			return this.validateNightStartTime(checkTimeIn);
		case NIGHT_END_TIME:
			return this.validateNightEndTime(checkTimeIn);
		default:
			return "时间timeType类型错误";
		}

	}

	/**
	 * 校验白天上班时间
	 * <p>Title: validateDayStartTime</p>
	 * <p>Description: </p>
	 * @param TimeIn
	 * @return
	 */
	public String validateDayStartTime(String TimeIn){
		if(TimeIn==null || TimeIn.trim().isEmpty()){
			return "白天上班时间不能为空";
		}
		try {
			dDayStartTime = sdf.parse(TimeIn);
			this.setDayStartTime(TimeIn);
			if(dDayStartTime==null){
				return "时间转换失败,请检查输入是否正确";
			}
			if(dDayStartTime.getHours()>=5){
				if(dNightEndTime!=null){
					if( (dNightEndTime.getHours()<=12 && dDayStartTime.getTime() >= dNightEndTime.getTime())
							|| (dNightEndTime.getHours()>12)	){
						return null;
					}else{
						return "白天上班时间不得小于晚上下班时间 " +sdf.format(this.dNightEndTime);
					}
				}else{
					return null;
				}
			}else{
				return "白天上班时间不得小于5:00";
			}
		} catch (ParseException e) {
			if(DEBUG)
				e.printStackTrace();
		}
		return "异常";//转换时间异常
	}

	/**
	 * 校验白天下班时间
	 * <p>Title: validateDayEndTime</p>
	 * <p>Description: </p>
	 * @param TimeIn
	 * @return
	 */
	public String validateDayEndTime(String TimeIn){
		if(TimeIn==null || TimeIn.trim().isEmpty()){
			return "白天下班时间不能为空";
		}
		try {
			dDayEndTime = sdf.parse(TimeIn);
			this.setDayEndTime(TimeIn);
			if(dDayEndTime==null){
				return "时间转换失败,请检查输入是否正确";
			}
			if(dDayStartTime==null)
				return "请先设置白天上班时间";
			if( dDayStartTime!=null && this.dDayEndTime.getTime() <= this.dDayStartTime.getTime())
				return "白天下班时间不得早于上班时间" +sdf.format(this.dDayStartTime);
			else
				return null;
		} catch (ParseException e) {
			if(DEBUG)
				e.printStackTrace();
		}
		return "异常";//转换时间异常
	}

	/**
	 * 校验晚上上班时间
	 * <p>Title: validateNightStartTime</p>
	 * <p>Description: 
	 * 		24>=st2>=et1 ||  st1>st2>=0
	 * </p>
	 * @param TimeIn
	 * @return
	 */
	public String validateNightStartTime(String TimeIn){
		if(TimeIn==null || TimeIn.trim().isEmpty()){
			return "晚上上班时间不能为空";
		}
		try {
			this.dNightStartTime = sdf.parse(TimeIn);
			this.setNightStartTime(TimeIn);
			if(dNightStartTime==null){
				return "时间转换失败,请检查输入是否正确";
			}
			if(dDayEndTime==null)
				return "请先设置白天下班时间";
			if( dNightStartTime.getHours()<=23 && dNightStartTime.getTime() >= this.dDayEndTime.getTime()){
				return null;
			}
			if( this.dDayStartTime==null)
				return "请先设置白天上班时间";
			if( this.dNightStartTime.getTime() > this.dDayStartTime.getTime() )//  dDayStartTime < dNightStartTime <= 23 
				return "晚上上班时间不得小于白天下班时间 "+sdf.format(this.dDayEndTime);
			//else  0<= dNightStartTime < dDayStartTime
			if( dNightStartTime.getHours()>=0 && dNightStartTime.getTime() < dDayStartTime.getTime())
				return null;
			else
				return "晚上上班时间不得大于白天上班时间"+sdf.format(dDayStartTime);
		} catch (ParseException e) {
			if(DEBUG)
				e.printStackTrace();
		}
		return "异常";//转换时间异常
	}

	/**
	 * 校验晚上下班时间
	 * <p>Title: validateNightEndTime</p>
	 * <p>Description: 
	 * 		24 >= et2 > st2   ||   st1 > et2 > st2
	 * </p>
	 * @param TimeIn
	 * @return
	 */
	public String validateNightEndTime(String TimeIn){
		if(TimeIn==null || TimeIn.trim().isEmpty()){
			return "晚上下班时间不能为空";
		}
		try {
			this.dNightEndTime = sdf.parse(TimeIn);
			this.setNightEndTime(TimeIn);
			if(dNightEndTime==null){
				return "时间转换失败,请检查输入是否正确";
			}
			if(dNightStartTime==null)
				return "请先设置晚上上班时间";
			if(this.dDayStartTime==null)
				return "请先设置白天上班时间";
			if( this.dNightStartTime.getTime() < this.dDayStartTime.getTime()){
				// 0 <= dNightStartTime < dDayStartTime
				if(dNightEndTime.getTime() > dDayStartTime.getTime() )
					return  "晚上下班时间不得早于白天上班时间 "+ sdf.format(this.dNightStartTime);
				return null;
			}else{
				// dDayEndTime <= dNightStartTime <= 24 
			}
			if( (dNightEndTime.getTime()>=0 && dNightEndTime.getTime()<= this.dDayStartTime.getTime()) ||
					(dNightEndTime.getHours()<=23 && dNightEndTime.getTime()>this.dNightStartTime.getTime()) ){
				return null;
			}else{
				return  "晚上下班时间不得早于晚上上班时间 "+ sdf.format(this.dNightStartTime);
			}

		} catch (ParseException e) {
			if(DEBUG)
				e.printStackTrace();
		}
		return "异常";//转换时间异常
	}

	// getter & setter

	public String getDayStartTime() {
		return dayStartTime;
	}

	public void setDayStartTime(String dayStartTime) {
		this.dayStartTime = dayStartTime;
		if(dayStartTime!=null){
			try{
				this.dDayStartTime = sdf.parse(this.dayStartTime);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}

	public String getDayEndTime() {
		return dayEndTime;
	}

	public void setDayEndTime(String dayEndTime) {
		this.dayEndTime = dayEndTime;
		if(dayEndTime!=null){
			try{
				this.dDayEndTime = sdf.parse(this.dayEndTime);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}

	public String getNightStartTime() {
		return nightStartTime;
	}

	public void setNightStartTime(String nightStartTime) {
		this.nightStartTime = nightStartTime;
		if(nightStartTime!=null){
			try{
				this.dNightStartTime = sdf.parse(this.nightStartTime);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}

	public String getNightEndTime() {
		return nightEndTime;
	}

	public void setNightEndTime(String nightEndTime) {
		this.nightEndTime = nightEndTime;
		if(nightEndTime!=null){
			try{
				this.dNightEndTime = sdf.parse(this.nightEndTime);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}



}
